package pers.w.chaptertalk.reader;

import android.annotation.SuppressLint;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import pers.w.chaptertalk.R;
import pers.w.chaptertalk.funs.IReader;

class StatusBar implements IReader {
    private TextView tv_clock, tv_pageCount, tv_chapterCount;
    private boolean stop = false;
    private ReaderActivity reader;


    void cancel() {
        stop = true;
    }

    StatusBar(ReaderActivity reader) {

        this.reader = reader;

        tv_clock = reader.findViewById(R.id.tv_clock);
        tv_pageCount = reader.findViewById(R.id.tv_pageCount);
        tv_chapterCount = reader.findViewById(R.id.tv_chapterCount);
    }


    void runUpdateTimeThread() {
        new Thread(() -> {
            while (!stop) {
                reader.handler.sendMessage(reader.handler.obtainMessage(HANDLER_UPDATE_STATUS));
                try {
                    Thread.sleep(1000);//1秒一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    void updateTime() {
        if (StatusBar.this.reader.hasWindowFocus()) {
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm ", Locale.getDefault());
            tv_clock.setText(simpleDateFormat.format(new Date()));
        }

    }

    @SuppressLint("SetTextI18n")
    void updatePage() {
        tv_pageCount.setText((reader.control.getCurPageIndex() + 1) + "/" + reader.control.getPagesSize());
    }

    @SuppressLint("SetTextI18n")
    void updateChapter() {
        tv_chapterCount.setText((reader.currentChapterIndex + 1) + "/" + reader.chapters.size());
    }

    @Override
    protected void finalize() throws Throwable {
        cancel();
        super.finalize();
    }
}